package net.xdclass.util;

import cn.hutool.core.convert.NumberChineseFormatter;
import cn.hutool.core.util.RandomUtil;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ITextPdfUtil {

    public static void main(String[] args) {

        List<String> totalList = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            //将数字i转为中文
            String chineseNumber =NumberChineseFormatter.format(i, false);
            String title = i + ".第" + chineseNumber + "题，以下答案正确的是（）。";
            totalList.add(title);
            totalList.add("A 选项A" + RandomUtil.randomString(10));
            totalList.add("B 选项B" + RandomUtil.randomString(10));
            totalList.add("C 选项C" + RandomUtil.randomString(10));
            totalList.add("D 选项D" + RandomUtil.randomString(10));
        }

        try {
            generatePdf(totalList, "D:\\home\\output.pdf");
            System.out.println("PDF created successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void generatePdfFile(String filePath){
        List<String> totalList = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            //将数字i转为中文
            String chineseNumber =NumberChineseFormatter.format(i, false);
            String title = i + ".第" + chineseNumber + "题，以下答案正确的是（）。";
            totalList.add(title);
            totalList.add("A 选项A" + RandomUtil.randomString(10));
            totalList.add("B 选项B" + RandomUtil.randomString(10));
            totalList.add("C 选项C" + RandomUtil.randomString(10));
            totalList.add("D 选项D" + RandomUtil.randomString(10));
        }

        try {
            generatePdf(totalList, filePath);
            System.out.println("PDF created successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void generatePdf(List<String> contentList, String dest) throws IOException {
        // 初始化 PdfWriter
        PdfWriter writer = new PdfWriter(dest);
        // 初始化 PdfDocument
        PdfDocument pdf = new PdfDocument(writer);
        // 初始化 Document
        Document document = new Document(pdf);

        // 加载中文字体
        PdfFont font = PdfFontFactory.createFont("D:\\home\\simkai.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);

        // 设置字体大小
        float titleFontSize = 18f; // 你可以根据需要调整字体大小
        float optionFontSize = 15f; // 你可以根据需要调整字体大小
        float currFontSize = 15f;

        // 遍历内容列表，将每个字符串添加为一个段落，并设置字体
        for (String content : contentList) {
            if (content.contains("以下答案正确的是")){
                currFontSize = titleFontSize;
            }else {
                currFontSize = optionFontSize;
            }
            Paragraph paragraph = new Paragraph(content).setFont(font).setFontSize(currFontSize);
            document.add(paragraph);
        }

        // 关闭文档
        document.close();
    }
}
